<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>message.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_proto2_message.js.html">message.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Protocol Buffer Message base class.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.proto2.Message&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.proto2.Descriptor&#39;);
<a name="line22"></a>goog.require(&#39;goog.proto2.FieldDescriptor&#39;);
<a name="line23"></a>goog.require(&#39;goog.proto2.Util&#39;);
<a name="line24"></a>goog.require(&#39;goog.string&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Abstract base class for all Protocol Buffer 2 messages. It will be
<a name="line30"></a> * subclassed in the code generated by the Protocol Compiler. Any other
<a name="line31"></a> * subclasses are prohibited.
<a name="line32"></a> * @constructor
<a name="line33"></a> */
<a name="line34"></a>goog.proto2.Message = function() {
<a name="line35"></a>  /**
<a name="line36"></a>   * Stores the field values in this message. Keyed by the tag of the fields.
<a name="line37"></a>   * @type {*}
<a name="line38"></a>   * @private
<a name="line39"></a>   */
<a name="line40"></a>  this.values_ = {};
<a name="line41"></a>
<a name="line42"></a>  /**
<a name="line43"></a>   * Stores the field information (i.e. metadata) about this message.
<a name="line44"></a>   * @type {Object.&lt;number, !goog.proto2.FieldDescriptor&gt;}
<a name="line45"></a>   * @private
<a name="line46"></a>   */
<a name="line47"></a>  this.fields_ = this.getDescriptor().getFieldsMap();
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * The lazy deserializer for this message instance, if any.
<a name="line51"></a>   * @type {goog.proto2.LazyDeserializer}
<a name="line52"></a>   * @private
<a name="line53"></a>   */
<a name="line54"></a>  this.lazyDeserializer_ = null;
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * A map of those fields deserialized, from tag number to their deserialized
<a name="line58"></a>   * value.
<a name="line59"></a>   * @type {Object}
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.deserializedFields_ = null;
<a name="line63"></a>};
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * An enumeration defining the possible field types.
<a name="line68"></a> * Should be a mirror of that defined in descriptor.h.
<a name="line69"></a> *
<a name="line70"></a> * TODO(user): Remove this alias.  The code generator generates code that
<a name="line71"></a> * references this enum, so it needs to exist until the code generator is
<a name="line72"></a> * changed.  The enum was moved to from Message to FieldDescriptor to avoid a
<a name="line73"></a> * dependency cycle.
<a name="line74"></a> *
<a name="line75"></a> * Use goog.proto2.FieldDescriptor.FieldType instead.
<a name="line76"></a> *
<a name="line77"></a> * @enum {number}
<a name="line78"></a> */
<a name="line79"></a>goog.proto2.Message.FieldType = {
<a name="line80"></a>  DOUBLE: 1,
<a name="line81"></a>  FLOAT: 2,
<a name="line82"></a>  INT64: 3,
<a name="line83"></a>  UINT64: 4,
<a name="line84"></a>  INT32: 5,
<a name="line85"></a>  FIXED64: 6,
<a name="line86"></a>  FIXED32: 7,
<a name="line87"></a>  BOOL: 8,
<a name="line88"></a>  STRING: 9,
<a name="line89"></a>  GROUP: 10,
<a name="line90"></a>  MESSAGE: 11,
<a name="line91"></a>  BYTES: 12,
<a name="line92"></a>  UINT32: 13,
<a name="line93"></a>  ENUM: 14,
<a name="line94"></a>  SFIXED32: 15,
<a name="line95"></a>  SFIXED64: 16,
<a name="line96"></a>  SINT32: 17,
<a name="line97"></a>  SINT64: 18
<a name="line98"></a>};
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * All instances of goog.proto2.Message should have a static descriptorObj_
<a name="line103"></a> * property. This is a JSON representation of a Descriptor. The real Descriptor
<a name="line104"></a> * will be deserialized lazily in the getDescriptor() method.
<a name="line105"></a> *
<a name="line106"></a> * This declaration is just here for documentation purposes.
<a name="line107"></a> * goog.proto2.Message does not have its own descriptor.
<a name="line108"></a> *
<a name="line109"></a> * @type {undefined}
<a name="line110"></a> * @private
<a name="line111"></a> */
<a name="line112"></a>goog.proto2.Message.descriptorObj_;
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * All instances of goog.proto2.Message should have a static descriptor_
<a name="line117"></a> * property. The Descriptor will be deserialized lazily in the getDescriptor()
<a name="line118"></a> * method.
<a name="line119"></a> *
<a name="line120"></a> * This declaration is just here for documentation purposes.
<a name="line121"></a> * goog.proto2.Message does not have its own descriptor.
<a name="line122"></a> *
<a name="line123"></a> * @type {undefined}
<a name="line124"></a> * @private
<a name="line125"></a> */
<a name="line126"></a>goog.proto2.Message.descriptor_;
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Initializes the message with a lazy deserializer and its associated data.
<a name="line131"></a> * This method should be called by internal methods ONLY.
<a name="line132"></a> *
<a name="line133"></a> * @param {goog.proto2.LazyDeserializer} deserializer The lazy deserializer to
<a name="line134"></a> *   use to decode the data on the fly.
<a name="line135"></a> *
<a name="line136"></a> * @param {*} data The data to decode/deserialize.
<a name="line137"></a> */
<a name="line138"></a>goog.proto2.Message.prototype.initializeForLazyDeserializer = function(
<a name="line139"></a>    deserializer, data) {
<a name="line140"></a>
<a name="line141"></a>  this.lazyDeserializer_ = deserializer;
<a name="line142"></a>  this.values_ = data;
<a name="line143"></a>  this.deserializedFields_ = {};
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * Sets the value of an unknown field, by tag.
<a name="line149"></a> *
<a name="line150"></a> * @param {number} tag The tag of an unknown field (must be &gt;= 1).
<a name="line151"></a> * @param {*} value The value for that unknown field.
<a name="line152"></a> */
<a name="line153"></a>goog.proto2.Message.prototype.setUnknown = function(tag, value) {
<a name="line154"></a>  goog.proto2.Util.assert(!this.fields_[tag],
<a name="line155"></a>                          &#39;Field is not unknown in this message&#39;);
<a name="line156"></a>
<a name="line157"></a>  goog.proto2.Util.assert(tag &gt;= 1, &#39;Tag is not valid&#39;);
<a name="line158"></a>  goog.proto2.Util.assert(value !== null, &#39;Value cannot be null&#39;);
<a name="line159"></a>
<a name="line160"></a>  this.values_[tag] = value;
<a name="line161"></a>  if (this.deserializedFields_) {
<a name="line162"></a>    delete this.deserializedFields_[tag];
<a name="line163"></a>  }
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Iterates over all the unknown fields in the message.
<a name="line169"></a> *
<a name="line170"></a> * @param {function(number, *)} callback A callback method
<a name="line171"></a> *     which gets invoked for each unknown field.
<a name="line172"></a> * @param {Object=} opt_scope The scope under which to execute the callback.
<a name="line173"></a> *     If not given, the current message will be used.
<a name="line174"></a> */
<a name="line175"></a>goog.proto2.Message.prototype.forEachUnknown = function(callback, opt_scope) {
<a name="line176"></a>  var scope = opt_scope || this;
<a name="line177"></a>  for (var key in this.values_) {
<a name="line178"></a>    var keyNum = Number(key);
<a name="line179"></a>    if (!this.fields_[keyNum]) {
<a name="line180"></a>      callback.call(scope, keyNum, this.values_[key]);
<a name="line181"></a>    }
<a name="line182"></a>  }
<a name="line183"></a>};
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Returns the descriptor which describes the current message.
<a name="line188"></a> *
<a name="line189"></a> * This only works if we assume people never subclass protobufs.
<a name="line190"></a> *
<a name="line191"></a> * @return {!goog.proto2.Descriptor} The descriptor.
<a name="line192"></a> */
<a name="line193"></a>goog.proto2.Message.prototype.getDescriptor = function() {
<a name="line194"></a>  // NOTE(nicksantos): These sorts of indirect references to descriptor
<a name="line195"></a>  // through this.constructor are fragile. See the comments
<a name="line196"></a>  // in set$Metadata for more info.
<a name="line197"></a>  var Ctor = this.constructor;
<a name="line198"></a>  return Ctor.descriptor_ ||
<a name="line199"></a>      (Ctor.descriptor_ = goog.proto2.Message.create$Descriptor(
<a name="line200"></a>          Ctor, Ctor.descriptorObj_));
<a name="line201"></a>};
<a name="line202"></a>
<a name="line203"></a>
<a name="line204"></a>/**
<a name="line205"></a> * Returns whether there is a value stored at the field specified by the
<a name="line206"></a> * given field descriptor.
<a name="line207"></a> *
<a name="line208"></a> * @param {goog.proto2.FieldDescriptor} field The field for which to check
<a name="line209"></a> *     if there is a value.
<a name="line210"></a> *
<a name="line211"></a> * @return {boolean} True if a value was found.
<a name="line212"></a> */
<a name="line213"></a>goog.proto2.Message.prototype.has = function(field) {
<a name="line214"></a>  goog.proto2.Util.assert(
<a name="line215"></a>      field.getContainingType() == this.getDescriptor(),
<a name="line216"></a>      &#39;The current message does not contain the given field&#39;);
<a name="line217"></a>
<a name="line218"></a>  return this.has$Value(field.getTag());
<a name="line219"></a>};
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * Returns the array of values found for the given repeated field.
<a name="line224"></a> *
<a name="line225"></a> * @param {goog.proto2.FieldDescriptor} field The field for which to
<a name="line226"></a> *     return the values.
<a name="line227"></a> *
<a name="line228"></a> * @return {!Array} The values found.
<a name="line229"></a> */
<a name="line230"></a>goog.proto2.Message.prototype.arrayOf = function(field) {
<a name="line231"></a>  goog.proto2.Util.assert(
<a name="line232"></a>      field.getContainingType() == this.getDescriptor(),
<a name="line233"></a>      &#39;The current message does not contain the given field&#39;);
<a name="line234"></a>
<a name="line235"></a>  return this.array$Values(field.getTag());
<a name="line236"></a>};
<a name="line237"></a>
<a name="line238"></a>
<a name="line239"></a>/**
<a name="line240"></a> * Returns the number of values stored in the given field.
<a name="line241"></a> *
<a name="line242"></a> * @param {goog.proto2.FieldDescriptor} field The field for which to count
<a name="line243"></a> *     the number of values.
<a name="line244"></a> *
<a name="line245"></a> * @return {number} The count of the values in the given field.
<a name="line246"></a> */
<a name="line247"></a>goog.proto2.Message.prototype.countOf = function(field) {
<a name="line248"></a>  goog.proto2.Util.assert(
<a name="line249"></a>      field.getContainingType() == this.getDescriptor(),
<a name="line250"></a>      &#39;The current message does not contain the given field&#39;);
<a name="line251"></a>
<a name="line252"></a>  return this.count$Values(field.getTag());
<a name="line253"></a>};
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * Returns the value stored at the field specified by the
<a name="line258"></a> * given field descriptor.
<a name="line259"></a> *
<a name="line260"></a> * @param {goog.proto2.FieldDescriptor} field The field for which to get the
<a name="line261"></a> *     value.
<a name="line262"></a> * @param {number=} opt_index If the field is repeated, the index to use when
<a name="line263"></a> *     looking up the value.
<a name="line264"></a> *
<a name="line265"></a> * @return {*} The value found or null if none.
<a name="line266"></a> */
<a name="line267"></a>goog.proto2.Message.prototype.get = function(field, opt_index) {
<a name="line268"></a>  goog.proto2.Util.assert(
<a name="line269"></a>      field.getContainingType() == this.getDescriptor(),
<a name="line270"></a>      &#39;The current message does not contain the given field&#39;);
<a name="line271"></a>
<a name="line272"></a>  return this.get$Value(field.getTag(), opt_index);
<a name="line273"></a>};
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>/**
<a name="line277"></a> * Returns the value stored at the field specified by the
<a name="line278"></a> * given field descriptor or the default value if none exists.
<a name="line279"></a> *
<a name="line280"></a> * @param {goog.proto2.FieldDescriptor} field The field for which to get the
<a name="line281"></a> *     value.
<a name="line282"></a> * @param {number=} opt_index If the field is repeated, the index to use when
<a name="line283"></a> *     looking up the value.
<a name="line284"></a> *
<a name="line285"></a> * @return {*} The value found or the default if none.
<a name="line286"></a> */
<a name="line287"></a>goog.proto2.Message.prototype.getOrDefault = function(field, opt_index) {
<a name="line288"></a>  goog.proto2.Util.assert(
<a name="line289"></a>      field.getContainingType() == this.getDescriptor(),
<a name="line290"></a>      &#39;The current message does not contain the given field&#39;);
<a name="line291"></a>
<a name="line292"></a>  return this.get$ValueOrDefault(field.getTag(), opt_index);
<a name="line293"></a>};
<a name="line294"></a>
<a name="line295"></a>
<a name="line296"></a>/**
<a name="line297"></a> * Stores the given value to the field specified by the
<a name="line298"></a> * given field descriptor. Note that the field must not be repeated.
<a name="line299"></a> *
<a name="line300"></a> * @param {goog.proto2.FieldDescriptor} field The field for which to set
<a name="line301"></a> *     the value.
<a name="line302"></a> * @param {*} value The new value for the field.
<a name="line303"></a> */
<a name="line304"></a>goog.proto2.Message.prototype.set = function(field, value) {
<a name="line305"></a>  goog.proto2.Util.assert(
<a name="line306"></a>      field.getContainingType() == this.getDescriptor(),
<a name="line307"></a>      &#39;The current message does not contain the given field&#39;);
<a name="line308"></a>
<a name="line309"></a>  this.set$Value(field.getTag(), value);
<a name="line310"></a>};
<a name="line311"></a>
<a name="line312"></a>
<a name="line313"></a>/**
<a name="line314"></a> * Adds the given value to the field specified by the
<a name="line315"></a> * given field descriptor. Note that the field must be repeated.
<a name="line316"></a> *
<a name="line317"></a> * @param {goog.proto2.FieldDescriptor} field The field in which to add the
<a name="line318"></a> *     the value.
<a name="line319"></a> * @param {*} value The new value to add to the field.
<a name="line320"></a> */
<a name="line321"></a>goog.proto2.Message.prototype.add = function(field, value) {
<a name="line322"></a>  goog.proto2.Util.assert(
<a name="line323"></a>      field.getContainingType() == this.getDescriptor(),
<a name="line324"></a>      &#39;The current message does not contain the given field&#39;);
<a name="line325"></a>
<a name="line326"></a>  this.add$Value(field.getTag(), value);
<a name="line327"></a>};
<a name="line328"></a>
<a name="line329"></a>
<a name="line330"></a>/**
<a name="line331"></a> * Clears the field specified.
<a name="line332"></a> *
<a name="line333"></a> * @param {goog.proto2.FieldDescriptor} field The field to clear.
<a name="line334"></a> */
<a name="line335"></a>goog.proto2.Message.prototype.clear = function(field) {
<a name="line336"></a>  goog.proto2.Util.assert(
<a name="line337"></a>      field.getContainingType() == this.getDescriptor(),
<a name="line338"></a>      &#39;The current message does not contain the given field&#39;);
<a name="line339"></a>
<a name="line340"></a>  this.clear$Field(field.getTag());
<a name="line341"></a>};
<a name="line342"></a>
<a name="line343"></a>
<a name="line344"></a>/**
<a name="line345"></a> * Compares this message with another one ignoring the unknown fields.
<a name="line346"></a> * @param {*} other The other message.
<a name="line347"></a> * @return {boolean} Whether they are equal. Returns false if the {@code other}
<a name="line348"></a> *     argument is a different type of message or not a message.
<a name="line349"></a> */
<a name="line350"></a>goog.proto2.Message.prototype.equals = function(other) {
<a name="line351"></a>  if (!other || this.constructor != other.constructor) {
<a name="line352"></a>    return false;
<a name="line353"></a>  }
<a name="line354"></a>
<a name="line355"></a>  var fields = this.getDescriptor().getFields();
<a name="line356"></a>  for (var i = 0; i &lt; fields.length; i++) {
<a name="line357"></a>    var field = fields[i];
<a name="line358"></a>    if (this.has(field) != other.has(field)) {
<a name="line359"></a>      return false;
<a name="line360"></a>    }
<a name="line361"></a>
<a name="line362"></a>    if (this.has(field)) {
<a name="line363"></a>      var isComposite = field.isCompositeType();
<a name="line364"></a>
<a name="line365"></a>      var fieldsEqual = function(value1, value2) {
<a name="line366"></a>        return isComposite ? value1.equals(value2) : value1 == value2;
<a name="line367"></a>      };
<a name="line368"></a>
<a name="line369"></a>      var thisValue = this.getValueForField_(field);
<a name="line370"></a>      var otherValue = other.getValueForField_(field);
<a name="line371"></a>
<a name="line372"></a>      if (field.isRepeated()) {
<a name="line373"></a>        // In this case thisValue and otherValue are arrays.
<a name="line374"></a>        if (thisValue.length != otherValue.length) {
<a name="line375"></a>          return false;
<a name="line376"></a>        }
<a name="line377"></a>        for (var j = 0; j &lt; thisValue.length; j++) {
<a name="line378"></a>          if (!fieldsEqual(thisValue[j], otherValue[j])) {
<a name="line379"></a>            return false;
<a name="line380"></a>          }
<a name="line381"></a>        }
<a name="line382"></a>      } else if (!fieldsEqual(thisValue, otherValue)) {
<a name="line383"></a>        return false;
<a name="line384"></a>      }
<a name="line385"></a>    }
<a name="line386"></a>  }
<a name="line387"></a>
<a name="line388"></a>  return true;
<a name="line389"></a>};
<a name="line390"></a>
<a name="line391"></a>
<a name="line392"></a>/**
<a name="line393"></a> * Recursively copies the known fields from the given message to this message.
<a name="line394"></a> * Removes the fields which are not present in the source message.
<a name="line395"></a> * @param {!goog.proto2.Message} message The source message.
<a name="line396"></a> */
<a name="line397"></a>goog.proto2.Message.prototype.copyFrom = function(message) {
<a name="line398"></a>  goog.proto2.Util.assert(this.constructor == message.constructor,
<a name="line399"></a>      &#39;The source message must have the same type.&#39;);
<a name="line400"></a>
<a name="line401"></a>  if (this != message) {
<a name="line402"></a>    this.values_ = {};
<a name="line403"></a>    if (this.deserializedFields_) {
<a name="line404"></a>      this.deserializedFields_ = {};
<a name="line405"></a>    }
<a name="line406"></a>    this.mergeFrom(message);
<a name="line407"></a>  }
<a name="line408"></a>};
<a name="line409"></a>
<a name="line410"></a>
<a name="line411"></a>/**
<a name="line412"></a> * Merges the given message into this message.
<a name="line413"></a> *
<a name="line414"></a> * Singular fields will be overwritten, except for embedded messages which will
<a name="line415"></a> * be merged. Repeated fields will be concatenated.
<a name="line416"></a> * @param {!goog.proto2.Message} message The source message.
<a name="line417"></a> */
<a name="line418"></a>goog.proto2.Message.prototype.mergeFrom = function(message) {
<a name="line419"></a>  goog.proto2.Util.assert(this.constructor == message.constructor,
<a name="line420"></a>      &#39;The source message must have the same type.&#39;);
<a name="line421"></a>  var fields = this.getDescriptor().getFields();
<a name="line422"></a>
<a name="line423"></a>  for (var i = 0; i &lt; fields.length; i++) {
<a name="line424"></a>    var field = fields[i];
<a name="line425"></a>    if (message.has(field)) {
<a name="line426"></a>      if (this.deserializedFields_) {
<a name="line427"></a>        delete this.deserializedFields_[field.getTag()];
<a name="line428"></a>      }
<a name="line429"></a>
<a name="line430"></a>      var isComposite = field.isCompositeType();
<a name="line431"></a>      if (field.isRepeated()) {
<a name="line432"></a>        var values = message.arrayOf(field);
<a name="line433"></a>        for (var j = 0; j &lt; values.length; j++) {
<a name="line434"></a>          this.add(field, isComposite ? values[j].clone() : values[j]);
<a name="line435"></a>        }
<a name="line436"></a>      } else {
<a name="line437"></a>        var value = message.getValueForField_(field);
<a name="line438"></a>        if (isComposite) {
<a name="line439"></a>          var child = this.getValueForField_(field);
<a name="line440"></a>          if (child) {
<a name="line441"></a>            child.mergeFrom(value);
<a name="line442"></a>          } else {
<a name="line443"></a>            this.set(field, value.clone());
<a name="line444"></a>          }
<a name="line445"></a>        } else {
<a name="line446"></a>          this.set(field, value);
<a name="line447"></a>        }
<a name="line448"></a>      }
<a name="line449"></a>    }
<a name="line450"></a>  }
<a name="line451"></a>};
<a name="line452"></a>
<a name="line453"></a>
<a name="line454"></a>/**
<a name="line455"></a> * @return {!goog.proto2.Message} Recursive clone of the message only including
<a name="line456"></a> *     the known fields.
<a name="line457"></a> */
<a name="line458"></a>goog.proto2.Message.prototype.clone = function() {
<a name="line459"></a>  var clone = new this.constructor;
<a name="line460"></a>  clone.copyFrom(this);
<a name="line461"></a>  return clone;
<a name="line462"></a>};
<a name="line463"></a>
<a name="line464"></a>
<a name="line465"></a>/**
<a name="line466"></a> * Fills in the protocol buffer with default values. Any fields that are
<a name="line467"></a> * already set will not be overridden.
<a name="line468"></a> * @param {boolean} simpleFieldsToo If true, all fields will be initialized;
<a name="line469"></a> *     if false, only the nested messages and groups.
<a name="line470"></a> */
<a name="line471"></a>goog.proto2.Message.prototype.initDefaults = function(simpleFieldsToo) {
<a name="line472"></a>  var fields = this.getDescriptor().getFields();
<a name="line473"></a>  for (var i = 0; i &lt; fields.length; i++) {
<a name="line474"></a>    var field = fields[i];
<a name="line475"></a>    var tag = field.getTag();
<a name="line476"></a>    var isComposite = field.isCompositeType();
<a name="line477"></a>
<a name="line478"></a>    // Initialize missing fields.
<a name="line479"></a>    if (!this.has(field) &amp;&amp; !field.isRepeated()) {
<a name="line480"></a>      if (isComposite) {
<a name="line481"></a>        this.values_[tag] = new /** @type {Function} */ (field.getNativeType());
<a name="line482"></a>      } else if (simpleFieldsToo) {
<a name="line483"></a>        this.values_[tag] = field.getDefaultValue();
<a name="line484"></a>      }
<a name="line485"></a>    }
<a name="line486"></a>
<a name="line487"></a>    // Fill in the existing composite fields recursively.
<a name="line488"></a>    if (isComposite) {
<a name="line489"></a>      if (field.isRepeated()) {
<a name="line490"></a>        var values = this.array$Values(tag);
<a name="line491"></a>        for (var j = 0; j &lt; values.length; j++) {
<a name="line492"></a>          values[j].initDefaults(simpleFieldsToo);
<a name="line493"></a>        }
<a name="line494"></a>      } else {
<a name="line495"></a>        this.get$Value(tag).initDefaults(simpleFieldsToo);
<a name="line496"></a>      }
<a name="line497"></a>    }
<a name="line498"></a>  }
<a name="line499"></a>};
<a name="line500"></a>
<a name="line501"></a>
<a name="line502"></a>/**
<a name="line503"></a> * Returns the field in this message by the given tag number. If no
<a name="line504"></a> * such field exists, throws an exception.
<a name="line505"></a> *
<a name="line506"></a> * @param {number} tag The field&#39;s tag index.
<a name="line507"></a> * @return {!goog.proto2.FieldDescriptor} The descriptor for the field.
<a name="line508"></a> * @private
<a name="line509"></a> */
<a name="line510"></a>goog.proto2.Message.prototype.getFieldByTag_ = function(tag) {
<a name="line511"></a>  goog.proto2.Util.assert(this.fields_[tag],
<a name="line512"></a>                          &#39;No field found for the given tag&#39;);
<a name="line513"></a>
<a name="line514"></a>  return this.fields_[tag];
<a name="line515"></a>};
<a name="line516"></a>
<a name="line517"></a>
<a name="line518"></a>/**
<a name="line519"></a> * Returns the whether or not the field indicated by the given tag
<a name="line520"></a> * has a value.
<a name="line521"></a> *
<a name="line522"></a> * GENERATED CODE USE ONLY. Basis of the has{Field} methods.
<a name="line523"></a> *
<a name="line524"></a> * @param {number} tag The tag.
<a name="line525"></a> *
<a name="line526"></a> * @return {boolean} Whether the message has a value for the field.
<a name="line527"></a> */
<a name="line528"></a>goog.proto2.Message.prototype.has$Value = function(tag) {
<a name="line529"></a>  goog.proto2.Util.assert(this.fields_[tag],
<a name="line530"></a>                          &#39;No field found for the given tag&#39;);
<a name="line531"></a>
<a name="line532"></a>  return tag in this.values_ &amp;&amp; goog.isDef(this.values_[tag]) &amp;&amp;
<a name="line533"></a>      this.values_[tag] !== null;
<a name="line534"></a>};
<a name="line535"></a>
<a name="line536"></a>
<a name="line537"></a>/**
<a name="line538"></a> * Returns the value for the given field. If a lazy deserializer is
<a name="line539"></a> * instantiated, lazily deserializes the field if required before returning the
<a name="line540"></a> * value.
<a name="line541"></a> *
<a name="line542"></a> * @param {goog.proto2.FieldDescriptor} field The field.
<a name="line543"></a> * @return {*} The field value, if any.
<a name="line544"></a> * @private
<a name="line545"></a> */
<a name="line546"></a>goog.proto2.Message.prototype.getValueForField_ = function(field) {
<a name="line547"></a>  // Retrieve the current value, which may still be serialized.
<a name="line548"></a>  var tag = field.getTag();
<a name="line549"></a>  if (!tag in this.values_) {
<a name="line550"></a>    return null;
<a name="line551"></a>  }
<a name="line552"></a>
<a name="line553"></a>  var value = this.values_[tag];
<a name="line554"></a>  if (value == null) {
<a name="line555"></a>    return null;
<a name="line556"></a>  }
<a name="line557"></a>
<a name="line558"></a>  // If we have a lazy deserializer, then ensure that the field is
<a name="line559"></a>  // properly deserialized.
<a name="line560"></a>  if (this.lazyDeserializer_) {
<a name="line561"></a>    // If the tag is not deserialized, then we must do so now. Deserialize
<a name="line562"></a>    // the field&#39;s value via the deserializer.
<a name="line563"></a>    if (!(tag in this.deserializedFields_)) {
<a name="line564"></a>      var deserializedValue = this.lazyDeserializer_.deserializeField(
<a name="line565"></a>          this, field, value);
<a name="line566"></a>      this.deserializedFields_[tag] = deserializedValue;
<a name="line567"></a>      return deserializedValue;
<a name="line568"></a>    }
<a name="line569"></a>
<a name="line570"></a>    return this.deserializedFields_[tag];
<a name="line571"></a>  }
<a name="line572"></a>
<a name="line573"></a>  // Otherwise, just return the value.
<a name="line574"></a>  return value;
<a name="line575"></a>};
<a name="line576"></a>
<a name="line577"></a>
<a name="line578"></a>/**
<a name="line579"></a> * Gets the value at the field indicated by the given tag.
<a name="line580"></a> *
<a name="line581"></a> * GENERATED CODE USE ONLY. Basis of the get{Field} methods.
<a name="line582"></a> *
<a name="line583"></a> * @param {number} tag The field&#39;s tag index.
<a name="line584"></a> * @param {number=} opt_index If the field is a repeated field, the index
<a name="line585"></a> *     at which to get the value.
<a name="line586"></a> *
<a name="line587"></a> * @return {*} The value found or null for none.
<a name="line588"></a> * @protected
<a name="line589"></a> */
<a name="line590"></a>goog.proto2.Message.prototype.get$Value = function(tag, opt_index) {
<a name="line591"></a>  var field = this.getFieldByTag_(tag);
<a name="line592"></a>  var value = this.getValueForField_(field);
<a name="line593"></a>
<a name="line594"></a>  if (field.isRepeated()) {
<a name="line595"></a>    goog.proto2.Util.assert(goog.isArray(value));
<a name="line596"></a>
<a name="line597"></a>    var index = opt_index || 0;
<a name="line598"></a>    goog.proto2.Util.assert(index &gt;= 0 &amp;&amp; index &lt; value.length,
<a name="line599"></a>        &#39;Given index is out of bounds&#39;);
<a name="line600"></a>
<a name="line601"></a>    return value[index];
<a name="line602"></a>  }
<a name="line603"></a>
<a name="line604"></a>  goog.proto2.Util.assert(!goog.isArray(value));
<a name="line605"></a>  return value;
<a name="line606"></a>};
<a name="line607"></a>
<a name="line608"></a>
<a name="line609"></a>/**
<a name="line610"></a> * Gets the value at the field indicated by the given tag or the default value
<a name="line611"></a> * if none.
<a name="line612"></a> *
<a name="line613"></a> * GENERATED CODE USE ONLY. Basis of the get{Field} methods.
<a name="line614"></a> *
<a name="line615"></a> * @param {number} tag The field&#39;s tag index.
<a name="line616"></a> * @param {number=} opt_index If the field is a repeated field, the index
<a name="line617"></a> *     at which to get the value.
<a name="line618"></a> *
<a name="line619"></a> * @return {*} The value found or the default value if none set.
<a name="line620"></a> * @protected
<a name="line621"></a> */
<a name="line622"></a>goog.proto2.Message.prototype.get$ValueOrDefault = function(tag, opt_index) {
<a name="line623"></a>
<a name="line624"></a>  if (!this.has$Value(tag)) {
<a name="line625"></a>    // Return the default value.
<a name="line626"></a>    var field = this.getFieldByTag_(tag);
<a name="line627"></a>    return field.getDefaultValue();
<a name="line628"></a>  }
<a name="line629"></a>
<a name="line630"></a>  return this.get$Value(tag, opt_index);
<a name="line631"></a>};
<a name="line632"></a>
<a name="line633"></a>
<a name="line634"></a>/**
<a name="line635"></a> * Gets the values at the field indicated by the given tag.
<a name="line636"></a> *
<a name="line637"></a> * GENERATED CODE USE ONLY. Basis of the {field}Array methods.
<a name="line638"></a> *
<a name="line639"></a> * @param {number} tag The field&#39;s tag index.
<a name="line640"></a> *
<a name="line641"></a> * @return {!Array} The values found. If none, returns an empty array.
<a name="line642"></a> * @protected
<a name="line643"></a> */
<a name="line644"></a>goog.proto2.Message.prototype.array$Values = function(tag) {
<a name="line645"></a>  goog.proto2.Util.assert(this.getFieldByTag_(tag).isRepeated(),
<a name="line646"></a>      &#39;Cannot call fieldArray on a non-repeated field&#39;);
<a name="line647"></a>  var field = this.getFieldByTag_(tag);
<a name="line648"></a>  var value = this.getValueForField_(field);
<a name="line649"></a>  goog.proto2.Util.assert(value == null || goog.isArray(value));
<a name="line650"></a>  return /** @type {Array} */ (value) || [];
<a name="line651"></a>};
<a name="line652"></a>
<a name="line653"></a>
<a name="line654"></a>/**
<a name="line655"></a> * Returns the number of values stored in the field by the given tag.
<a name="line656"></a> *
<a name="line657"></a> * GENERATED CODE USE ONLY. Basis of the {field}Count methods.
<a name="line658"></a> *
<a name="line659"></a> * @param {number} tag The tag.
<a name="line660"></a> *
<a name="line661"></a> * @return {number} The number of values.
<a name="line662"></a> * @protected
<a name="line663"></a> */
<a name="line664"></a>goog.proto2.Message.prototype.count$Values = function(tag) {
<a name="line665"></a>  var field = this.getFieldByTag_(tag);
<a name="line666"></a>
<a name="line667"></a>  if (field.isRepeated()) {
<a name="line668"></a>    if (this.has$Value(tag)) {
<a name="line669"></a>      goog.proto2.Util.assert(goog.isArray(this.values_[tag]));
<a name="line670"></a>    }
<a name="line671"></a>
<a name="line672"></a>    return this.has$Value(tag) ? this.values_[tag].length : 0;
<a name="line673"></a>  } else {
<a name="line674"></a>    return this.has$Value(tag) ? 1 : 0;
<a name="line675"></a>  }
<a name="line676"></a>};
<a name="line677"></a>
<a name="line678"></a>
<a name="line679"></a>/**
<a name="line680"></a> * Sets the value of the *non-repeating* field indicated by the given tag.
<a name="line681"></a> *
<a name="line682"></a> * GENERATED CODE USE ONLY. Basis of the set{Field} methods.
<a name="line683"></a> *
<a name="line684"></a> * @param {number} tag The field&#39;s tag index.
<a name="line685"></a> * @param {*} value The field&#39;s value.
<a name="line686"></a> * @protected
<a name="line687"></a> */
<a name="line688"></a>goog.proto2.Message.prototype.set$Value = function(tag, value) {
<a name="line689"></a>  if (goog.proto2.Util.conductChecks()) {
<a name="line690"></a>    var field = this.getFieldByTag_(tag);
<a name="line691"></a>
<a name="line692"></a>    goog.proto2.Util.assert(!field.isRepeated(),
<a name="line693"></a>                            &#39;Cannot call set on a repeated field&#39;);
<a name="line694"></a>
<a name="line695"></a>    this.checkFieldType_(field, value);
<a name="line696"></a>  }
<a name="line697"></a>
<a name="line698"></a>  this.values_[tag] = value;
<a name="line699"></a>  if (this.deserializedFields_) {
<a name="line700"></a>    this.deserializedFields_[tag] = value;
<a name="line701"></a>  }
<a name="line702"></a>};
<a name="line703"></a>
<a name="line704"></a>
<a name="line705"></a>/**
<a name="line706"></a> * Adds the value to the *repeating* field indicated by the given tag.
<a name="line707"></a> *
<a name="line708"></a> * GENERATED CODE USE ONLY. Basis of the add{Field} methods.
<a name="line709"></a> *
<a name="line710"></a> * @param {number} tag The field&#39;s tag index.
<a name="line711"></a> * @param {*} value The value to add.
<a name="line712"></a> * @protected
<a name="line713"></a> */
<a name="line714"></a>goog.proto2.Message.prototype.add$Value = function(tag, value) {
<a name="line715"></a>  if (goog.proto2.Util.conductChecks()) {
<a name="line716"></a>    var field = this.getFieldByTag_(tag);
<a name="line717"></a>
<a name="line718"></a>    goog.proto2.Util.assert(field.isRepeated(),
<a name="line719"></a>                            &#39;Cannot call add on a non-repeated field&#39;);
<a name="line720"></a>
<a name="line721"></a>    this.checkFieldType_(field, value);
<a name="line722"></a>  }
<a name="line723"></a>
<a name="line724"></a>  if (!this.values_[tag]) {
<a name="line725"></a>    this.values_[tag] = [];
<a name="line726"></a>  }
<a name="line727"></a>
<a name="line728"></a>  this.values_[tag].push(value);
<a name="line729"></a>  if (this.deserializedFields_) {
<a name="line730"></a>    delete this.deserializedFields_[tag];
<a name="line731"></a>  }
<a name="line732"></a>};
<a name="line733"></a>
<a name="line734"></a>
<a name="line735"></a>/**
<a name="line736"></a> * Ensures that the value being assigned to the given field
<a name="line737"></a> * is valid.
<a name="line738"></a> *
<a name="line739"></a> * @param {!goog.proto2.FieldDescriptor} field The field being assigned.
<a name="line740"></a> * @param {*} value The value being assigned.
<a name="line741"></a> * @private
<a name="line742"></a> */
<a name="line743"></a>goog.proto2.Message.prototype.checkFieldType_ = function(field, value) {
<a name="line744"></a>  goog.proto2.Util.assert(value !== null);
<a name="line745"></a>
<a name="line746"></a>  var nativeType = field.getNativeType();
<a name="line747"></a>  if (nativeType === String) {
<a name="line748"></a>    goog.proto2.Util.assert(typeof value === &#39;string&#39;,
<a name="line749"></a>                            &#39;Expected value of type string&#39;);
<a name="line750"></a>  } else if (nativeType === Boolean) {
<a name="line751"></a>    goog.proto2.Util.assert(typeof value === &#39;boolean&#39;,
<a name="line752"></a>                            &#39;Expected value of type boolean&#39;);
<a name="line753"></a>  } else if (nativeType === Number) {
<a name="line754"></a>    goog.proto2.Util.assert(typeof value === &#39;number&#39;,
<a name="line755"></a>                            &#39;Expected value of type number&#39;);
<a name="line756"></a>  } else if (field.getFieldType() ==
<a name="line757"></a>             goog.proto2.FieldDescriptor.FieldType.ENUM) {
<a name="line758"></a>    goog.proto2.Util.assert(typeof value === &#39;number&#39;,
<a name="line759"></a>                            &#39;Expected an enum value, which is a number&#39;);
<a name="line760"></a>  } else {
<a name="line761"></a>    goog.proto2.Util.assert(value instanceof nativeType,
<a name="line762"></a>                            &#39;Expected a matching message type&#39;);
<a name="line763"></a>  }
<a name="line764"></a>};
<a name="line765"></a>
<a name="line766"></a>
<a name="line767"></a>/**
<a name="line768"></a> * Clears the field specified by tag.
<a name="line769"></a> *
<a name="line770"></a> * GENERATED CODE USE ONLY. Basis of the clear{Field} methods.
<a name="line771"></a> *
<a name="line772"></a> * @param {number} tag The tag of the field to clear.
<a name="line773"></a> * @protected
<a name="line774"></a> */
<a name="line775"></a>goog.proto2.Message.prototype.clear$Field = function(tag) {
<a name="line776"></a>  goog.proto2.Util.assert(this.getFieldByTag_(tag), &#39;Unknown field&#39;);
<a name="line777"></a>  delete this.values_[tag];
<a name="line778"></a>  if (this.deserializedFields_) {
<a name="line779"></a>    delete this.deserializedFields_[tag];
<a name="line780"></a>  }
<a name="line781"></a>};
<a name="line782"></a>
<a name="line783"></a>
<a name="line784"></a>/**
<a name="line785"></a> * Creates the metadata descriptor representing the definition of this message.
<a name="line786"></a> *
<a name="line787"></a> * GENERATED CODE USE ONLY. Called when constructing message classes.
<a name="line788"></a> *
<a name="line789"></a> * @param {function(new:goog.proto2.Message)} messageType Constructor for the
<a name="line790"></a> *     message type to which this metadata applies.
<a name="line791"></a> * @param {Object} metadataObj The object containing the metadata.
<a name="line792"></a> * @return {!goog.proto2.Descriptor} The new descriptor.
<a name="line793"></a> */
<a name="line794"></a>goog.proto2.Message.create$Descriptor = function(messageType, metadataObj) {
<a name="line795"></a>  var fields = [];
<a name="line796"></a>  var descriptorInfo;
<a name="line797"></a>
<a name="line798"></a>  for (var key in metadataObj) {
<a name="line799"></a>    if (!metadataObj.hasOwnProperty(key)) {
<a name="line800"></a>      continue;
<a name="line801"></a>    }
<a name="line802"></a>
<a name="line803"></a>    goog.proto2.Util.assert(goog.string.isNumeric(key), &#39;Keys must be numeric&#39;);
<a name="line804"></a>
<a name="line805"></a>    if (key == 0) {
<a name="line806"></a>      descriptorInfo = metadataObj[0];
<a name="line807"></a>      continue;
<a name="line808"></a>    }
<a name="line809"></a>
<a name="line810"></a>    // Create the field descriptor.
<a name="line811"></a>    fields.push(
<a name="line812"></a>        new goog.proto2.FieldDescriptor(messageType, key, metadataObj[key]));
<a name="line813"></a>  }
<a name="line814"></a>
<a name="line815"></a>  goog.proto2.Util.assert(descriptorInfo);
<a name="line816"></a>  return new goog.proto2.Descriptor(messageType, descriptorInfo, fields);
<a name="line817"></a>};
<a name="line818"></a>
<a name="line819"></a>
<a name="line820"></a>/**
<a name="line821"></a> * Sets the metadata that represents the definition of this message.
<a name="line822"></a> *
<a name="line823"></a> * GENERATED CODE USE ONLY. Called when constructing message classes.
<a name="line824"></a> *
<a name="line825"></a> * @param {!Function} messageType Constructor for the
<a name="line826"></a> *     message type to which this metadata applies.
<a name="line827"></a> * @param {Object} metadataObj The object containing the metadata.
<a name="line828"></a> */
<a name="line829"></a>goog.proto2.Message.set$Metadata = function(messageType, metadataObj) {
<a name="line830"></a>  // NOTE(nicksantos): JSCompiler&#39;s type-based optimizations really do not
<a name="line831"></a>  // like indirectly defined methods (both prototype methods and
<a name="line832"></a>  // static methods). This is very fragile in compiled code. I think it only
<a name="line833"></a>  // really works by accident, and is highly likely to break in the future.
<a name="line834"></a>  messageType.descriptorObj_ = metadataObj;
<a name="line835"></a>  messageType.getDescriptor = function() {
<a name="line836"></a>    // The descriptor is created lazily when we instantiate a new instance.
<a name="line837"></a>    return messageType.descriptor_ ||
<a name="line838"></a>        (new messageType()).getDescriptor();
<a name="line839"></a>  };
<a name="line840"></a>};
</pre>


</body>
</html>
